# coding = utf-8
from selenium import webdriver
from selenium.webdriver import ActionChains
import time
import pymysql
config = {
    'host': 'localhost',
    'user': 'root',
    'password': '123456',
    'port': 3306,
    'charset': 'utf8',
    'database': 'text'
}
con = pymysql.connect(**config)
cursor = con.cursor()
print('连接数据库成功')
cursor.execute("drop table if exists 期末测试表")
cursor.execute("""

                 CREATE TABLE 期末测试表 (
                 Email_id int(12) NOT NULL AUTO_INCREMENT,
                 Email_user varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
                 Email_title text CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
                 Email_content text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL,
                 Email_time text CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
                 PRIMARY KEY (Email_id) USING BTREE)

                 ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
                 """
               )
print("创建成功")
username='3152143438'
password='hfyhfy520'
browser = webdriver.Firefox()
browser.get('https://mail.qq.com/cgi-bin/loginpage')
browser.switch_to.frame('login_frame') #找到邮箱账号登录框对应的iframe
browser.find_element_by_xpath('//*[@id="u"]').send_keys(username)
time.sleep(1)
browser.find_element_by_xpath('//*[@id="p"]').send_keys(password)
time.sleep(1)
browser.find_element_by_class_name('login_button').click() #点击登陆按钮
#模拟点击事件
browser.find_element_by_xpath('//*[@id="login_button"]').click()
print("已经成功进入邮箱")
time.sleep(10)
browser.switch_to.default_content()
username = browser.find_element_by_id('folder_1')
username.click()
browser.switch_to.default_content()
name = browser.find_element_by_id("useraddr").text
print(name)
if name == ('3152143438@qq.com'):
    print('已经进入邮件页面')

else:
    print('登录失败')

main_frame = browser.find_element_by_id('mainFrame')
browser.switch_to.frame(main_frame)


h = browser.current_window_handle

email_eles = browser.find_elements_by_css_selector('.toarea .F, .toarea .M')
email_eles_count = len(email_eles)


for i in range(email_eles_count):

    email_ele = browser.find_elements_by_css_selector('.toarea .F, .toarea .M')[i]

    ActionChains(browser).context_click(email_ele).perform()

    browser.find_elements_by_css_selector('.menu_item')[1].click()

    email_detail_handle = browser.window_handles[1]
    browser.switch_to.window(email_detail_handle)
    print("-------------------------------------------------------------------")
    main_frame = browser.find_element_by_id("mainFrame")
    browser.switch_to.frame(main_frame)
    time.sleep(10)
    user = browser.find_element_by_class_name("grn").text
    print("发件人：", user)
    subject = browser.find_element_by_id('subject').text

    print("主题：", subject)

    data = browser.find_element_by_id('local-time-caption').text

    print("发件时间：", data)

    try:
        content = browser.find_element_by_id('mailContentContainer').text
        print("邮件内容：", content)
    except:
        content = browser.find_element_by_id('QQMAILSTATIONERY').text
        print("邮件内容：", content)
    print("正在写入邮件")
    sql = 'insert into 期末测试表(Email_user,Email_title,Email_content,Email_time) values(%s,%s,%s,%s)'

    # 写入数据库
    #     print("发件人：{} ------邮件标题：{}-----发件时间：{} -----发件内容：{}").format(user,subject,data,content)
    # 关闭tab页
    con.commit()
    print("邮件写入成功")
    Email_user = user
    Email_title = subject
    Email_content = content
    Email_time = data
    values = (Email_user, Email_title, Email_content, Email_time)
    cursor.execute(sql, values)

    browser.close()
    # 回到第一个tab页，即邮件列表tab
    browser.switch_to.window(h)
    # 又切换到列表部分
    main_frame = browser.find_element_by_id('mainFrame')
    browser.switch_to.frame(main_frame)
browser.quit()